<?php

class AccountRepository {

    var $db;

    function AccountRepository() {
        global $SERVER, $DATABASE, $USERNAME, $PASSWORD;
        $this->db = new Connection($SERVER, $DATABASE, $USERNAME, $PASSWORD);
    }

    function GetAllAccount() {
        $query = "SELECT * FROM Account";
        $result = $this->db->ExecuteQuery($query);
        $array = array();
        $i = 0;
        if ($result) {
            while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                $acc = new AccountEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]);
                $array[$i] = $acc;
                $i++;
            }
            return $array;
        } else {
            return null;
        }
    }

    function GetAccountByUserName($username) {
        $query = "SELECT * FROM Account WHERE Username_s like '" . $username . "'";
        $result = $this->db->ExecuteQuery($query);
        if ($result) {
            while ($row = mysql_fetch_array($result)) {
                $acc = new AccountEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]);
                return $acc;
            }
        } else {
            return null;
        }
    }

    function CheckAccountAndPassword($username, $password) {
        $query = "SELECT * FROM Account WHERE Username_s = '" . $username . "' AND Password_s = '" . $password . "'";
        $result = $this->db->ExecuteQuery($query);
        while ($row = mysql_fetch_array($result)) {
            return TRUE;
        }
        return FALSE;
    }

    function CheckAccount($username) {
        $query = "SELECT * FROM Account WHERE Username_s = '" . $username . "'";
        $result = $this->db->ExecuteQuery($query);
        while ($row = mysql_fetch_array($result)) {
            return TRUE;
        }
        return FALSE;
    }

    function GetGroupByID($GID) {
        $query = "SELECT Name_s FROM `group` WHERE ID_i =" . $GID;
        $result = $this->db->ExecuteQuery($query);
        while ($row = mysql_fetch_array($result)) {
            return $row[0];
        }
        return NULL;
    }

    function GetAllGroup() {
        $query = "SELECT * FROM `group`";
        $result = $this->db->ExecuteQuery($query);
        $array = array();
        $count = 0;
        while ($row = mysql_fetch_array($result)) {
            $group = new GroupEntity($row[0], $row[1]);
            $array[$count++] = $group;
        }
        return $array;
    }

    function UpdatePassword($name, $pass) {
        $query = "UPDATE Account SET Password_s = '" . $pass . "' WHERE Username_s = '" . $name . "'";
        $result = $this->db->ExecuteQuery($query);
        if ($result) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //Account Manager---------------------------------------------------------------

    function Insert($username, $pass, $email, $group, $yahoo, $hotro) {
        $query = "INSERT INTO account (Username_s, Password_s, Email_s, GroupID_i, YahooID_s, HoTro_b) VALUES";
        $query.="('" . $username . "',";
        $query.="'" . $pass . "',";
        $query.="'" . $email . "',";
        $query.="'" . $group . "',";
        $query.="'" . $yahoo . "',";
        $query.="b'" . $hotro . "')";
        $result = $this->db->ExecuteQuery($query);
        if ($result) {
            return TRUE;
        }
        return FALSE;
    }

    function Update($username, $pass, $email, $group, $yahoo, $hotro) {
        $query = "UPDATE account SET Password_s='" . $pass . "',
                                     Email_s='" . $email . "',
                                     GroupID_i=" . $group . ",
                                     YahooID_s='" . $yahoo . "',
                                     HoTro_b =" . $hotro . "
                                     WHERE Username_s ='" . $username . "'";
        $result = $this->db->ExecuteQuery($query);
        if ($result) {
            return TRUE;
        }
        return FALSE;
    }

    function Detele($username) {
        $query = "DELETE FROM Account WHERE Username_s='" . $username . "'";
        $result = $this->db->ExecuteQuery($query) or die(mysql_error());
        if ($result) {
            return TRUE;
        }
        return FALSE;
    }

    function GetAccount_HoTro()
    {
        $query = "SELECT * FROM account WHERE HoTro_b = 1";
        $result = $this->db->ExecuteQuery($query);
        $array = array();
        $i = 0;
        if ($result) {
            while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                $acc = new AccountEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]);
                $array[$i++] = $acc;
            }
            return $array;
        } 
        return null;
    }
}

?>
